namespace Hospital.Domain
{
    /// <summary>
    /// 工作流服务类
    /// </summary>
    public static class WorkflowService
    {
        /// <summary>
        /// 创建服务审核工作流
        /// </summary>
        /// <param name="doctorId">医生Id</param>
        /// <param name="serviceTypes">服务类型列表</param>
        /// <returns>工作流实例</returns>
        public static ServiceAuditWorkflow CreateServiceAuditWorkflow(int doctorId, List<ServiceType> serviceTypes)
        {
            var workflow = new ServiceAuditWorkflow
            {
                DoctorId = doctorId,
                Status = "进行中",
                CreatedTime = DateTime.Now
            };

            // 为每个服务类型创建审核步骤
            for (int i = 0; i < serviceTypes.Count; i++)
            {
                var step = new ServiceAuditStep
                {
                    WorkflowId = workflow.Id, // 这里会在保存后更新
                    ServiceName = serviceTypes[i].Name,
                    AuditStatus = "未审核",
                    StepOrder = i + 1
                };
                workflow.Steps.Add(step);
            }

            return workflow;
        }

        /// <summary>
        /// 更新工作流步骤
        /// </summary>
        /// <param name="workflow">工作流</param>
        /// <param name="stepId">步骤Id</param>
        /// <param name="auditStatus">审核状态</param>
        /// <param name="remarks">备注</param>
        /// <param name="auditor">审核人</param>
        public static void UpdateWorkflowStep(ServiceAuditWorkflow workflow, int stepId, string auditStatus, string? remarks, string? auditor)
        {
            var step = workflow.Steps.FirstOrDefault(s => s.Id == stepId);
            if (step != null)
            {
                step.AuditStatus = auditStatus;
                step.Remarks = remarks;
                step.Auditor = auditor;
                step.AuditTime = DateTime.Now;
            }
        }

        /// <summary>
        /// 检查工作流是否完成
        /// </summary>
        /// <param name="workflow">工作流</param>
        /// <returns>是否所有步骤都通过</returns>
        public static bool IsWorkflowCompleted(ServiceAuditWorkflow workflow)
        {
            if (!workflow.Steps.Any())
                return false;

            return workflow.Steps.All(s => s.AuditStatus == "审核通过");
        }

        /// <summary>
        /// 检查工作流是否被拒绝
        /// </summary>
        /// <param name="workflow">工作流</param>
        /// <returns>是否有步骤被拒绝</returns>
        public static bool IsWorkflowRejected(ServiceAuditWorkflow workflow)
        {
            return workflow.Steps.Any(s => s.AuditStatus == "审核不通过");
        }

        /// <summary>
        /// 完成工作流
        /// </summary>
        /// <param name="workflow">工作流</param>
        public static void CompleteWorkflow(ServiceAuditWorkflow workflow)
        {
            if (IsWorkflowCompleted(workflow))
            {
                workflow.Status = "已完成";
                workflow.CompletedTime = DateTime.Now;
            }
            else if (IsWorkflowRejected(workflow))
            {
                workflow.Status = "已拒绝";
                workflow.CompletedTime = DateTime.Now;
            }
        }

        /// <summary>
        /// 获取工作流状态描述
        /// </summary>
        /// <param name="workflow">工作流</param>
        /// <returns>状态描述</returns>
        public static string GetWorkflowStatusDescription(ServiceAuditWorkflow workflow)
        {
            if (IsWorkflowCompleted(workflow))
                return "已通过";
            else if (IsWorkflowRejected(workflow))
                return "已拒绝";
            else
                return "待审核";
        }
    }
} 